// pages/order-list/order-list.js
import {
  Order
} from "../../models/order.js"
import {
  Enum
} from "../../utils/enum.js"
var order = new Order()
Page({
  data: {
    orderList: [],
    orderType: "",
    currentPage: 1,
    stopMore: false,
    showNoOrderData: false
  },

  onLoad: function(options) {
    order.showLoading()
    this.setPageTitle(options.orderType)
    this.setData({
      orderType: options.orderType
    })

    order.getOrderList({
      type: options.orderType
    }, (res) => {
      this.getOrderListRes(res)
      wx.hideLoading()
    })
  },

  onShow: function() {
    var update = wx.getStorageSync(Enum.fromOrderList)
    // order list的状态有更新 
    if (update) {
      wx.setStorageSync(Enum.fromOrderList, false)
      order.getOrderList({
        type: this.data.orderType
      }, (res) => {
        this.getOrderListRes(res)
      })
    }
  },

  setPageTitle(orderType) {
    var titleName = '买书订单'
    if (orderType == Enum.orderType.recycleOrder) {
      var titleName = '卖书订单'
    }
    wx.setNavigationBarTitle({
      title: titleName
    })
  },

  getOrderListRes(res) {
    // 判断res
    if (order.processRes(res.statusCode)) {
      var dataArray = res.data.data.data
      // 有订单
      if (dataArray) {
        var result = this.setBtnAttr(dataArray)
        this.setData({
          orderList: result,
          showNoOrderData: false
        })
      } else {
        // 没有订单
        this.setData({
          showNoOrderData: true
        })
      }
    }
  },

  setBtnAttr(dataArray) {
    for (var i = 0; i < dataArray.length; i++) {
      var item = dataArray[i]
      var result = order.getOrderText(item.status, item.order_type)
      item.btnDisable = result.disable,
        item.statusText = result.text
    }
    return dataArray
  },

  orderDetail(event) {
    var orderID = event.currentTarget.dataset.id
    var orderStatus = event.currentTarget.dataset.status
    // 跳转到order detail
    wx.navigateTo({
      url: '/pages/order/order?orderID=' + orderID + '&orderStatus=' + orderStatus + "&orderType=" + this.data.orderType
    })
  },

  orderStateTap(event) {
    var index = event.currentTarget.dataset.index
    var orderList = this.data.orderList

    order.operateOrder(orderList[index], Enum.fromOrderList,
      () => {
        order.toast("支付成功")
        // 支付成功后  更新 order list 的snap 状态
        this.updateSnapStatus(orderList, index, "PAID")
      })
  },


  updateSnapStatus(orderList, index, key){
    // 更新 order list 部分snap 状态
    orderList[index].status = Enum.orderStatus[key]
    var result = order.getOrderText(orderList[index].status, orderList[index].order_type)
    orderList[index].btnDisable = result.disable,
      orderList[index].statusText = result.text

    this.setData({
      orderList: orderList
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (!this.data.stopMore) {
      order.showLoading()
      var param = {
        type: this.data.orderType,
        page: this.data.currentPage + 1
      }
      order.getOrderList(param, (res) => {
        if (order.processRes(res.statusCode)) {
          // 有数据
          if (res.data.data.data) {
            // btn attr
            var resData = this.setBtnAttr(res.data.data.data)
            res.data.data.data = resData
          }
          var result = order.updataList(res.data, this.data.orderList)
          this.setData({
            currentPage: result.currentPage,
            orderList: result.data,
            stopMore: result.stopMore
          })
        }
        // else 服务器没有返回正确的信息  状态码不是2开头
        wx.hideLoading()
      })
      
    } else {
      order.toast("没有更多了", "none")
    }
  }
})